package com.pdg.dungeondelver.configuration;

import java.io.InputStream;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;
import org.newdawn.slick.util.xml.XMLElement;
import org.newdawn.slick.util.xml.XMLElementList;
import org.newdawn.slick.util.xml.XMLParser;


public class Configuration {
	private static final String TAG_THEME = "theme";
	private static final String DEFAULT_THEME = "default";
	private static final String TAG_LANGUAGE = "language";
	private static final String DEFAULT_LANGUAGE = "en-US";
	private static final String TAG_THEMES_CONFIGURATION = "themes-configuration";
	private static final String DEFAULT_THEMES_CONFIGURATION = "cfg/themes.xml";
	
	private Themes themes = null;
	public Themes getThemes(){
		if(themes==null){
			try {
				themes = new Themes(this,getThemesConfiguration());
			} catch (ThemesLoadException e) {
				e.printStackTrace();
				themes = null;
			}
		}
		return themes;
	}
	
	private Theme getCurrentTheme(){
		return getThemes().getTheme(getTheme());
	}
	
	private String theme = DEFAULT_THEME;
	public String getTheme(){
		return theme;
	}
	private void setTheme(String theTheme){
		theme = theTheme;
	}
	public Configuration(String ref) throws ConfigurationLoadException{
		if(!loadFromXml(ref)){
			throw new ConfigurationLoadException();
		}
	}
	private String language = DEFAULT_LANGUAGE;
	public String getLanguage(){
		return language;
	}
	private void setLanguage(String content) {
		language = content;
	}
	
	private String themesConfiguration = DEFAULT_THEMES_CONFIGURATION;
	public String getThemesConfiguration(){
		return themesConfiguration;
	}
	public void setThemesConfiguration(String theThemesConfiguration){
		themesConfiguration = theThemesConfiguration;
	}
	
	private Boolean loadFromXml(String ref){
		XMLParser parser = new XMLParser();
		try {
			InputStream stream = ResourceLoader.getResourceAsStream(ref);
			XMLElement root = parser.parse(ref,stream);
			XMLElementList children = root.getChildren();
			for(Integer index=0;index<children.size();++index){
				XMLElement child = children.get(index);
				if(child.getName().compareTo(TAG_THEME)==0){
					setTheme(child.getContent());
				}else if(child.getName().compareTo(TAG_LANGUAGE)==0){
					setLanguage(child.getContent());
				}else if(child.getName().compareTo(TAG_THEMES_CONFIGURATION)==0){
					setThemesConfiguration(child.getContent());
				}
			}
			return true;
		} catch (SlickException e) {
			return false;
		}
	}

	public PropertySerializers getPropertySerializers() {
		return getCurrentTheme().getPropertySerializers();
	}

	public DescriptorTable getPropertyGroups() {
		return getCurrentTheme().getPropertyGroups();
	}

	public ImageManager getImageManager() {
		return getCurrentTheme().getImageManager();
	}

	public MapManager getMaps() {
		return getCurrentTheme().getMaps();
	}

	public DescriptorTable getTerrains() {
		return getCurrentTheme().getTerrains();
	}
	
}
